import { computed } from 'vue'
// 将mapState换成mapGetters即可封装成useGetters的hook
import { useStore, mapState } from 'vuex'

const store = useStore()
export function useState(mapper) {
  // 返回的是{key1: fn1, key2: fn2}
  const stateStoreFns = mapState(mapper)
  const storeState = {}
  Object.keys(stateStoreFns).forEach((fnKey) => {
    // 使用mapState时，会自动去找this.$store,手动绑定一下this (this就是随便一个对象，但是改对象需要有$store属性并且值为创建的store)
    const fn = stateStoreFns[fnKey].bind({ $store: store })
    storeState[fnKey] = computed(fn)
  })
  return storeState
}
